#include "head.h"

int in_the_map(const int a, const int b) {
  return a >= 0 && b >= 0 && a <= MAPSIZE && b <= MAPSIZE;
}
int bfs(const int cur_x, const int cur_y, const int find_coin) {
  memset(vis, 0, sizeof(vis));
  memset(record, 0, sizeof(record));
  memset(queuex, 0, sizeof(queuex));
  memset(queuey, 0, sizeof(queuey));
  memset(path, 0, sizeof(path));
  memset(bucket, 0, sizeof(bucket));
  head = 0, tail = 1;
  int cnt = 0;
  queuex[1] = cur_x;
  queuey[1] = cur_y;
  vis[cur_x][cur_y] = 1;
  while (head < tail) {
    ++head;
    for (int i = 1; i <= 4; ++i) {
      int nextx = queuex[head] + movex[i];
      int nexty = queuey[head] + movey[i];
      if (in_the_map(nextx, nexty) && !vis[nextx][nexty])
        if ((4 <= map[nextx][nexty] && map[nextx][nexty] <= 6) ||
            map[nextx][nexty] == 0) {
          queuex[++tail] = nextx;
          queuey[tail] = nexty;
          vis[nextx][nexty] = 1;
          record[nextx][nexty] = move_num[i];
          if (4 <= map[nextx][nexty] && map[nextx][nexty] <= 5)
            ++cnt;
          if (map[nextx][nexty] == 6 && key > 0)
            ++cnt;
          if (cnt == find_coin)
            return 1;
        }
    }
  }
  return 0;
}
int my_rand(const int from, const int to) {
  int len = to - from + 1;
  return rand() % len + from;
}
void make_it_vis(int x, int y) { visible[x][y] = 1; }

void init_card() {
  HideCursor();
  memset(map, 0, sizeof(map));
  memset(visible, 0, sizeof(visible));
  srand((unsigned)time(NULL));
  for (int i = 1; i <= 8; ++i) {
    int repeat = my_rand(8, 13);
    for (int j = 1; j <= repeat; ++j) {
      map[my_rand(0, MAXX)][my_rand(0, MAXX)] = i;
    }
  }
  make_it_vis(nowx, nowy);
  map[nowx][nowy] = 0;
  memset(bucket, 0, sizeof(bucket));
  for (int j = MAPSIZE; j >= 0; --j)
    for (int i = 0; i <= MAPSIZE; ++i)
      ++bucket[map[i][j]];
  for (int i = 0; i <= 8; ++i)
    printf("%s=%d\n", name[i], bucket[i]);
  printf("total coin=%d\n", bucket[5] + min(bucket[4], bucket[6]));
  if (bucket[5] + min(bucket[4], bucket[6]) < 10) {
    mySleep(1000);
    system("clear");
    init_card();
  }
}
double f1(int x) { return -(1 / 40) * (x - 30) + 2; }
double f2(int x) { return 100.0 / x; }
double f(int coin, int padlock, int key, double time, int tot_step, int life) {
  return (coin * 1e5 + padlock * 25000 + key * 5000) * f1(time) * f2(tot_step) *
         (life * 0.1);
}
int cmp(struct User a, struct User b) {
  if (a.score < b.score)
    return 1;
  if (a.score > b.score)
    return 0;
  if (strcmp(a.name, b.name) > 0)
    return 1;
  return 0;
}

void insert_sort(struct User tmp) {
  FILE *out = fopen("data", "rb");
  struct User t[100];
  int tot_user = 0;
  if (out != NULL) {
    while (!feof(out))
      fread(&t[++tot_user], sizeof(t[tot_user]), 1, out);
    fclose(out);
    --tot_user; //ͳ�һ�
  }

  int insert = 1;
  for (insert = 1; insert <= tot_user; ++insert)
    if (cmp(t[insert], tmp))
      break; //Ѱ�Ҳ�λ�
  --insert;

  FILE *in = fopen("data", "wb");
  for (int i = 1; i <= insert; ++i) {
    fwrite(&t[i], sizeof(t[i]), 1, in);
    output(&t[i], i);
  }

  fwrite(&tmp, sizeof(tmp), 1, in);

  setcolor(60); //////////////////
  output(&tmp, insert + 1);
  setcolor(55); /////////////////

  for (int i = insert + 1; i <= tot_user; ++i) {
    fwrite(&t[i], sizeof(t[i]), 1, in);
    output(&t[i], i + 1);
  }
  fclose(in);
}
void project() {
  clock_t start, end;
  start = clock();
  system("color 07");
  full_screen();
  system("clear");
  init_card();
  for (;;) {
    if (needcls)
      display_card();
    else
      needcls = 1;
    if (p)
      Move_character(path[p--]);
    else
      move_character();
    if (life <= 0) {
      display_card();
      puts("You lose");
      break;
    } else if (coin + padlock >= 10) {
      display_card();
      puts("You win");
      break;
    }
  }
  end = clock();
  double time = (double)(end - start) / CLOCKS_PER_SEC;
  printf("time=%f\n", (double)(end - start) / CLOCKS_PER_SEC);
  system("pause");
  double score = f(coin, padlock, key, time, tot_steps, life);
  printf("Your score: %f\n", score);
  win();
  char name[100];
  scanf("%s", name);
  struct User now;
  strcpy(now.name, name);
  now.score = score;
  now.coin = coin;
  now.padlock = padlock;
  now.key = key;
  now.time = time;
  now.tot_step = tot_steps;
  now.life = life;
  insert_sort(now);
}
void show_rank() {
  system("clear");
  FILE *in = fopen("data", "rb");
  if (in == NULL)
    puts("No local data found,please play the game first!");
  else {
    struct User tmp;
    fread(&tmp, sizeof(tmp), 1, in);
    int cnt = 0;
    while (!feof(in)) {
      output(&tmp, ++cnt);
      fread(&tmp, sizeof(tmp), 1, in);
    }
  }
  fclose(in);
  enter_the_game();
}
